﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Excel = Microsoft.Office.Interop.Excel;
using System.Windows.Forms;

namespace ExcelAddInMongolianConverter
{
    public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void About_Click(object sender, RibbonControlEventArgs e)
        {
            MongolianConverter.AboutUs frmAbout = new MongolianConverter.AboutUs();
            frmAbout.Show();
        }

        private void arial_to_arialmon_Click(object sender, RibbonControlEventArgs e)
        {
            string text = "";
            string str;
            int i = 0;
            int rCnt = 0;
            int cCnt = 0;

            Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet);
            Excel.Range range = activeWorksheet.get_Range(Globals.ThisAddIn.Application.ActiveWindow.RangeSelection.Address, System.Type.Missing);

            for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    text = "";
                    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                    //MessageBox.Show(str);
                    if (str != null)
                    {
                        for (i = 1; i <= str.Length; i++)
                        {
                            string Char = str.Substring(i - 1, 1);
                            //MessageBox.Show(Char);
                            var uni_code = (int)Convert.ToChar(Char);
                            string ascii_code = "";
                            char ascii_text;
                            if (uni_code == 1025)
                            {
                                ascii_code = "168";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1028)
                            {
                                ascii_code = "170";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1256)
                            {
                                ascii_code = "170";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1198)
                            {
                                ascii_code = "175";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1031)
                            {
                                ascii_code = "175";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1105)
                            {
                                ascii_code = "184";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1108)
                            {
                                ascii_code = "186";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1257)
                            {
                                ascii_code = "186";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1111)
                            {
                                ascii_code = "191";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code == 1199)
                            {
                                ascii_code = "191";
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else if (uni_code >= 1040 && uni_code <= 1103)
                            {
                                ascii_code = (uni_code - 848).ToString();
                                ascii_text = (char)Int32.Parse(ascii_code);
                                text = text + ascii_text;
                            }
                            else
                            {
                                text = text + Char;
                            }
                        }
                        range.Cells[rCnt, cCnt] = text;
                    }
                }
            }
            MessageBox.Show("Амжилттай хөрвүүлж дууслаа.", "Мэдээлэл", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void arialmon_to_arial_Click(object sender, RibbonControlEventArgs e)
        {
            string text = "";
            string str;
            int i = 0;
            int rCnt = 0;
            int cCnt = 0;

            Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet);
            Excel.Range range = activeWorksheet.get_Range(Globals.ThisAddIn.Application.ActiveWindow.RangeSelection.Address, System.Type.Missing);

            for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    text = "";
                    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                    //MessageBox.Show(str);
                    if (str != null)
                    {
                        for (i = 1; i <= str.Length; i++)
                        {
                            string Char = str.Substring(i - 1, 1);
                            //MessageBox.Show(Char);
                            var ascii_code = (int)Convert.ToChar(Char);
                            string unicode_code = "";
                            char unicode_text;
                            if (ascii_code == 168)
                            {
                                unicode_code = "1025";
                                unicode_text = (char)Int32.Parse(unicode_code);
                                text = text + unicode_text;
                            }
                            else if (ascii_code == 170)
                            {
                                unicode_code = "1256";
                                unicode_text = (char)Int32.Parse(unicode_code);
                                text = text + unicode_text;
                            }
                            else if (ascii_code == 175)
                            {
                                unicode_code = "1198";
                                unicode_text = (char)Int32.Parse(unicode_code);
                                text = text + unicode_text;
                            }
                            else if (ascii_code == 184)
                            {
                                unicode_code = "1105";
                                unicode_text = (char)Int32.Parse(unicode_code);
                                text = text + unicode_text;
                            }
                            else if (ascii_code == 186)
                            {
                                unicode_code = "1257";
                                unicode_text = (char)Int32.Parse(unicode_code);
                                text = text + unicode_text;
                            }
                            else if (ascii_code == 191)
                            {
                                unicode_code = "1199";
                                unicode_text = (char)Int32.Parse(unicode_code);
                                text = text + unicode_text;
                            }
                            else if (ascii_code >= 192 && ascii_code <= 255)
                            {
                                unicode_code = (ascii_code + 848).ToString();
                                unicode_text = (char)Int32.Parse(unicode_code);
                                text = text + unicode_text;
                            }
                            else
                            {
                                text = text + Char;
                            }
                        }
                        range.Cells[rCnt, cCnt] = text;
                    }
                }
            }
            MessageBox.Show("Амжилттай хөрвүүлж дууслаа.", "Мэдээлэл", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void unicode_to_latin_Click(object sender, RibbonControlEventArgs e)
        {
            string text = "";
            string str;
            int i = 0;
            int rCnt = 0;
            int cCnt = 0;

            Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet);
            Excel.Range range = activeWorksheet.get_Range(Globals.ThisAddIn.Application.ActiveWindow.RangeSelection.Address, System.Type.Missing);

            for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
            {
                for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
                {
                    text = "";
                    str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
                    //MessageBox.Show(str);
                    if (str != null)
                    {
                        for (i = 1; i <= str.Length; i++)
                        {
                            string Char = str.Substring(i - 1, 1);
                            //MessageBox.Show(Char);
                            var uni_code = (int)Convert.ToChar(Char);
                            string latin_code = "";

                            if (uni_code == 1040) { latin_code = "A"; text = text + latin_code; }
                            else if (uni_code == 1041) { latin_code = "B"; text = text + latin_code; }
                            else if (uni_code == 1042) { latin_code = "V"; text = text + latin_code; }
                            else if (uni_code == 1043) { latin_code = "G"; text = text + latin_code; }
                            else if (uni_code == 1044) { latin_code = "D"; text = text + latin_code; }
                            else if (uni_code == 1045) { latin_code = "YE"; text = text + latin_code; }
                            else if (uni_code == 1025) { latin_code = "YO"; text = text + latin_code; }
                            else if (uni_code == 1046) { latin_code = "J"; text = text + latin_code; }
                            else if (uni_code == 1047) { latin_code = "Z"; text = text + latin_code; }
                            else if (uni_code == 1048) { latin_code = "I"; text = text + latin_code; }
                            else if (uni_code == 1049) { latin_code = "I"; text = text + latin_code; }
                            else if (uni_code == 1050) { latin_code = "K"; text = text + latin_code; }
                            else if (uni_code == 1051) { latin_code = "L"; text = text + latin_code; }
                            else if (uni_code == 1052) { latin_code = "M"; text = text + latin_code; }
                            else if (uni_code == 1053) { latin_code = "N"; text = text + latin_code; }
                            else if (uni_code == 1054) { latin_code = "O"; text = text + latin_code; }
                            else if (uni_code == 1256) { latin_code = "U"; text = text + latin_code; }
                            else if (uni_code == 1055) { latin_code = "P"; text = text + latin_code; }
                            else if (uni_code == 1056) { latin_code = "R"; text = text + latin_code; }
                            else if (uni_code == 1057) { latin_code = "S"; text = text + latin_code; }
                            else if (uni_code == 1058) { latin_code = "T"; text = text + latin_code; }
                            else if (uni_code == 1059) { latin_code = "U"; text = text + latin_code; }
                            else if (uni_code == 1198) { latin_code = "O"; text = text + latin_code; }
                            else if (uni_code == 1060) { latin_code = "F"; text = text + latin_code; }
                            else if (uni_code == 1061) { latin_code = "KH"; text = text + latin_code; }
                            else if (uni_code == 1062) { latin_code = "TS"; text = text + latin_code; }
                            else if (uni_code == 1063) { latin_code = "CH"; text = text + latin_code; }
                            else if (uni_code == 1064) { latin_code = "SH"; text = text + latin_code; }
                            else if (uni_code == 1065) { latin_code = "SH"; text = text + latin_code; }
                            else if (uni_code == 1066) { latin_code = "'"; text = text + latin_code; }
                            else if (uni_code == 1067) { latin_code = "Y"; text = text + latin_code; }
                            else if (uni_code == 1068) { latin_code = "I"; text = text + latin_code; }
                            else if (uni_code == 1069) { latin_code = "E"; text = text + latin_code; }
                            else if (uni_code == 1070) { latin_code = "YU"; text = text + latin_code; }
                            else if (uni_code == 1071) { latin_code = "YA"; text = text + latin_code; }
                            else if (uni_code == 1072) { latin_code = "a"; text = text + latin_code; }
                            else if (uni_code == 1073) { latin_code = "b"; text = text + latin_code; }
                            else if (uni_code == 1074) { latin_code = "v"; text = text + latin_code; }
                            else if (uni_code == 1075) { latin_code = "g"; text = text + latin_code; }
                            else if (uni_code == 1076) { latin_code = "d"; text = text + latin_code; }
                            else if (uni_code == 1077) { latin_code = "ye"; text = text + latin_code; }
                            else if (uni_code == 1105) { latin_code = "yo"; text = text + latin_code; }
                            else if (uni_code == 1078) { latin_code = "j"; text = text + latin_code; }
                            else if (uni_code == 1079) { latin_code = "z"; text = text + latin_code; }
                            else if (uni_code == 1080) { latin_code = "i"; text = text + latin_code; }
                            else if (uni_code == 1081) { latin_code = "i"; text = text + latin_code; }
                            else if (uni_code == 1082) { latin_code = "k"; text = text + latin_code; }
                            else if (uni_code == 1083) { latin_code = "l"; text = text + latin_code; }
                            else if (uni_code == 1084) { latin_code = "m"; text = text + latin_code; }
                            else if (uni_code == 1085) { latin_code = "n"; text = text + latin_code; }
                            else if (uni_code == 1086) { latin_code = "o"; text = text + latin_code; }
                            else if (uni_code == 1257) { latin_code = "u"; text = text + latin_code; }
                            else if (uni_code == 1087) { latin_code = "p"; text = text + latin_code; }
                            else if (uni_code == 1088) { latin_code = "r"; text = text + latin_code; }
                            else if (uni_code == 1089) { latin_code = "s"; text = text + latin_code; }
                            else if (uni_code == 1090) { latin_code = "t"; text = text + latin_code; }
                            else if (uni_code == 1091) { latin_code = "u"; text = text + latin_code; }
                            else if (uni_code == 1199) { latin_code = "o"; text = text + latin_code; }
                            else if (uni_code == 1092) { latin_code = "f"; text = text + latin_code; }
                            else if (uni_code == 1093) { latin_code = "kh"; text = text + latin_code; }
                            else if (uni_code == 1094) { latin_code = "ts"; text = text + latin_code; }
                            else if (uni_code == 1095) { latin_code = "ch"; text = text + latin_code; }
                            else if (uni_code == 1096) { latin_code = "sh"; text = text + latin_code; }
                            else if (uni_code == 1097) { latin_code = "sh"; text = text + latin_code; }
                            else if (uni_code == 1098) { latin_code = "'"; text = text + latin_code; }
                            else if (uni_code == 1099) { latin_code = "y"; text = text + latin_code; }
                            else if (uni_code == 1100) { latin_code = "i"; text = text + latin_code; }
                            else if (uni_code == 1101) { latin_code = "e"; text = text + latin_code; }
                            else if (uni_code == 1102) { latin_code = "yu"; text = text + latin_code; }
                            else if (uni_code == 1103) { latin_code = "ya"; text = text + latin_code; }
                            else { text = text + Char; }
                        }
                        range.Cells[rCnt, cCnt] = text;
                    }
                }
            }
            MessageBox.Show("Амжилттай хөрвүүлж дууслаа.", "Мэдээлэл", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
